using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

using System.Web.Security;
using Prodma.DataAccess.Models;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Authentication;

namespace Prodma.Panel.Controllers
{
    public class AccountController : Controller
    {

        #region LogOn
        public ActionResult LogOn()
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("index", "home");
            }

            HttpCookie logOnCookies = Request.Cookies["logOnCookies"];
            if (logOnCookies != null)
            {
                ViewData["userName"] = logOnCookies["username"];
                ViewData["password"] = logOnCookies["pwd"];
                ViewData["isRemember"] = logOnCookies["remember"];
            }

            return View();
        }
        #endregion

        #region LogOut
        public ActionResult LogOut()
        {
            // Remove loginUser in loginUserList
            LoginUser current = (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];
            List<LoginUser> list = (List<LoginUser>)System.Web.HttpContext.Current.Application["loginUserList"];
            list.Remove(current);
            list.TrimExcess();
            System.Web.HttpContext.Current.Application["loginUserList"] = list;
            ////////////////////////////////////

            FormsAuthentication.SignOut();

            return RedirectToAction("Logon", "Account");
        }
        #endregion

        #region Authentication
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Authentication()
        {
            string userName = Request.Form["userName"];
            string password = Request.Form["password"];

            HttpCookie logOnCookies = new HttpCookie("logOnCookies");
            if (Request.Form["rememberMe"] == "on")
            {
                logOnCookies["username"] = userName;
                logOnCookies["pwd"] = password;
                logOnCookies["remember"] = "true";
                logOnCookies.Expires = DateTime.Now.AddDays(30);
                Response.Cookies.Add(logOnCookies);
            }
            else
            {
                Response.Cookies.Remove("logOnCookies");
            }

            DataAccess.Authentication.Authentication auth = new Authentication();

            bool isValid = auth.ControlAuthentication(userName, password);

            if (isValid)
            {
                FormsAuthentication.SetAuthCookie(userName, false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        #endregion


        #region Authorization Error
        public ActionResult AuthorizationError()
        {
            return View();
        }
        #endregion

    }
}
